import axios  from "axios";
import {Notification} from "element-ui";
import router from "@/router";
import el from "element-ui/src/locale/lang/el";


const instace=axios.create({
    baseURL:'http://127.0.0.1:8081',
    timeout:15000
})

//请求拦截
instace.interceptors.request.use(
    function (config){
    if (localStorage.getItem('Token')){
        config.headers.Authorization=localStorage.getItem('Token');
    }else if (/auth/.test(config.url)){
        return config
    }else{
        console.log('token无法获取')
    }
    return config;
}
)
//响应拦截器
instace.interceptors.response.use(
    function (res) {
        const data=res.data
        switch (data.code) {
            case "4011":
            case "4013":
            case "4014":
            case "4015":
            case "4016":
                router.push('../login')
            case "4010":
            case "4012":
                Notification.error({
                    title: "提示信息",
                    message: data.msg,
                });
                break;
        }
        return res;

    },
    function (err) {
        const  data=err.response.data
        //特殊情况处理
        switch (data.code) {
            case "4011":
            case "4013":
            case "4014":
            case "4015":
            case "4016":
                router.push('../login')
            case "4010":
            case "4012":
                Notification.error({
                    title: "提示信息",
                    message: data.msg,
                });
                break;
        }
        return Promise.reject(err)

    }
)

export function get(url,params) {
    return instace.get(url,{
        params
    })

}
export function  post(url,data,config) {
    return instace.post(url,data,config)
}

//post请求,Content-Type为application/x-www-form-urlencoded
export function formDataPost(url,data) {
    const config = {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        transformRequest : [function (data) {
            let ret = ''
            for (let it in data) {
                ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
            }
            return ret
        }]
    }
    return instance.post(url,data,config)
}

export function reqFuc(data) {
    return instance.request({
        url: '/api/order/download', // 接口
        method: 'post',  //
        responseType: 'blob', // 注意:这里必须指定返回类型,否则打不开文件
        data:data
    })
}

export function reqIsolation(data) {
    return instance.request({
        url: '/api/isolationInfo/download', // 接口
        method: 'post',  //
        responseType: 'blob', // 注意:这里必须指定返回类型,否则打不开文件
        data:data
    })
}

export function logDownload(data) {
    return instance.request({
        url: '/api/logs/download', // 接口
        method: 'post',  //
        responseType: 'blob', // 注意:这里必须指定返回类型,否则打不开文件
        data:data
    })
}
